#!/usr/bin/expect -f

set USER zhouyunfei
set HOST fort.hupu.com
set PASSWORD Java4ever2018

set GROUP [lindex $argv 0]
set SERVER [lindex $argv 1]
set SU [lindex $argv 2]
set CD [lindex $argv 3]

spawn ssh $USER@$HOST
expect "password:" {
    send "$PASSWORD\n"
}
expect "Select group:" {
    send "$GROUP\n"
}
if { $SERVER != "" } {
    expect "Select server:" {
        send "$SERVER\n"
    }
}
expect "]#" {
    if { $SU != "" } {
        send "su - $SU\n"
        expect "]\\$" {
            if { $CD != "" } {
                send "$CD\n"
            }
        }
    }
}
interact
